<?php
class Mozg_Templator
{
    /**
 		базовый путь к шаблонам
     */
    private $_isCache = true;

    protected $_detector;
    
    public function __construct(Mozg_Detector $detector = null)
    {
    	$this->_detector = new Mozg_Detector($this, $detector);
    	$this->RequireLang();
    }

    /**
	* вкл/выкл кэш шаблонов
	*/
    final public function CacheTurnOff()
    {
        $this->_isCache = false;
        return $this;
    }

    final public function CacheTurnOn()
    {
        $this->_isCache = true;
        return $this;
    }

    protected function RequireLang()
    {
		foreach ($this->_detector as $component)
    	{
    		Mozg_Lang::Required('module_' .strtolower($component[0]). '_ctrl_' .strtolower($component[1]));
    	}
    }
    
    protected function render($fileName, $isFullPath = false)
    {
        ob_start();
        include($this->GetRealFile($fileName, $isFullPath));
        return ob_get_clean(); // filter output
    }
    
    private function CleanContent($content)
    {
	    //return str_replace("\t", '', $content);
        return Mozg::Lib('minifier')->MinifyHTML($content);
    }

    private function DetectRealFilePath($fileName)
    {
    	foreach ($this->_detector as $component)
    	{
    		$filePath = _APP_PATH.($component[0] ? 'Module/' .ucfirst($component[0]). '/' : ''). 'templates/' .$fileName;
    		if (is_file($filePath)) return $filePath;
    	}

    	throw new Mozg_Exception_NotFound(Mozg_Lang::_('Template ' .$filePath. ' not found in %s', get_class($this)));
    }
    
    protected function GetTpl($name = '')
    {
    	foreach ($this->_detector as $component)
    	{
    		try
    		{
    			return Mozg::Tpl($name, $component[0], $component[1], $this->_detector);
    		}
    		catch (Exception $e)
    		{
    			
    		}
    	}

    	throw new Mozg_Exception_NotFound(Mozg_Lang::_('Template ' .$name. ' not found in %s', get_class($this)));
    }
    
    private function GetRealFile($fileName, $isFullPath = false)
    {
    	if ($isFullPath)
    	{
    		$filePath = $fileName;
    	}
    	else
    	{
    		$filePath = $this->DetectRealFilePath($fileName);
    	}
    	
        if (!Mozg_Cfg::Get('global')->GetIsTmpCache() || !$this->_isCache) return $filePath;
                
        if (0 === strpos($filePath, _ROOT_PATH)) $filePath = substr($filePath, strlen(_ROOT_PATH));
		$filePath = ltrim($filePath, '/\\');
        
        $tmpCachePath = _CACHE_PATH_TMP;
        
        if (!is_file($tmpCachePath.$filePath))
        {
            if (Mozg::Lib('dir')->Create($tmpCachePath.pathinfo($filePath, PATHINFO_DIRNAME)))
            {
                file_put_contents($tmpCachePath.$filePath, $this->CleanContent(file_get_contents(_ROOT_PATH.$filePath)));
            }
        }

        return $tmpCachePath.$filePath;
    }

    private $_style;
    
    final public function Style()
    {
    	if (!$this->_style)
    	{
    		$this->_style = Mozg::Ctrl('media', 'style', 'http', 'web');
    	}
    	
    	return $this->_style;
    }
}